package com.yinkgh.mybatis.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.formula.udf.UDFFinder;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Name;
import org.apache.poi.ss.usermodel.PictureData;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yinkgh.mybatis.service.PerformanceAssessmentService;
import com.yinkgh.mybatis.view.PerformanceAssessmentView;
import com.yinkgh.util.ExcelUtil;

/**
 * 绩效考核
 * @author yinlong
 *
 */
@Controller
@RequestMapping("/performance")
public class PerformanceAssessmentController {
	public Logger logger = Logger.getLogger(PerformanceAssessmentController.class);
	@Autowired
	private PerformanceAssessmentService assessmentService;
	
	@RequestMapping(value="/getPerformance", produces = "application/json; charset=utf-8")
	@ResponseBody
	public Map<String, Object> getPerformance(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		PerformanceAssessmentView performanceAssessmentView = assessmentService.getPerformance(1);
		map.put("performanceAssessmentView", performanceAssessmentView);
		return map;
	}
	
	@RequestMapping(value="/exportPerformance", produces = "application/json; charset=utf-8")
	@ResponseBody
	public String exportPerformance(HttpServletRequest request, HttpServletResponse response) {
		String result = "error";
			try {
				Workbook workbook = assessmentService.getPerformanceAssessmentExcel(1, ExcelUtil.TYPE_XLSX);
				ExcelUtil.getInstance().downloadExcel(request, response, workbook, ExcelUtil.TYPE_XLSX);
				result = "success";
			} catch (IOException e) {
				e.printStackTrace();
				logger.error(e.getMessage(), e);
			} catch (InvalidFormatException e) {
				e.printStackTrace();
				logger.error(e.getMessage(), e);
			} catch (Exception e) {
				e.printStackTrace();
				logger.error(e.getMessage(), e);
			} 
		return result;
	}
}
